<template>
  <div class="login">
    <el-form ref="ref" :model="form" :rules="rules">
      <img class="img" src="@/assets/common/login-logo.png" alt="">
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item prop="password">
        <!-- 组件标签想使用原生方法 @原生事件名,native="" -->
        <el-input
          v-model="form.password"
          show-password
          prefix-icon="el-icon-unlock"
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-button class="submit" @click="submit">登录</el-button>
      <div class="tip">还没有账号？立即注册</div>
    </el-form>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      form: {
        mobile: '13800000002', //	string	必须		手机号
        password: '123456' //	string	必须		密码
      },
      rules: {
        mobile: [
          {
            require: true,
            message: '必填',
            trigger: 'change'
          },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'change'
          }
        ],
        password: [
          {
            require: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 6,
            max: 12,
            message: '请输入6——12位数密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.ref.validate(async(result) => {
        // 执行异步代码 如果登录成功执行下面代码 如果登录失败 下面代码不会执行
        await this.$store.dispatch('user/toLogin', this.form)
        this.$message.success('登录成功') // 在登录完成后执行
        this.$router.push(this.$route.query.redirect || '/')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-image: url(~@/assets/common/login.jpg);
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .img {
    padding-bottom: 25px;
    width: 100%;
  }
  .submit {
    margin-top: 20px;
    width: 100%;
    border: 1px solid #3e7efd;
    background-color: #3e7efd;
    text-align: center;
    color: #fff;
  }
  .tip {
    padding-top: 30px;
    text-align: center;
    color: #fff;
  }
  ::v-deep .el-form-item__error {
    color: #fff;
  }
}
</style>
